SVGWeather = {};

// See SVGWeather.Dial.defaults for values supplied in options
SVGWeather.Dial = function Dial( options )
{
	if ( options ) this.mergeWith( options );
	this.mergeWith( arguments.callee.defaults );
	
	this.svg = document.getElementById( 'dial-template' ).cloneNode( true );
	this.svg.setAttribute( 'class', 'dial' );
	this.svg.setAttribute( 'transform', "translate("+this.centerX+","+this.centerY+") scale("+this.scale+")" )

	var angleSweep = this.maxAngle - this.minAngle;
	var valueSweep = this.maxValue - this.minValue;
	this.anglePerValue = angleSweep / valueSweep;
	
	var template = this.svg.getElementByClassName( 'minor-tick' );
	if ( this.minorTicks && template )
	{
		for ( var angle = this.minAngle; angle<=this.maxAngle; angle+=this.anglePerValue*this.minorTicks )
		{
			var item = ( angle == this.minAngle ) ? template : template.cloneNode( true );
			item.setAttribute( 'transform', 'rotate('+angle+')' );
			if ( item != template ) template.parentNode.appendChild( item );
		}
	}
	
	var template = this.svg.getElementByClassName( 'major-tick' );
	if ( this.majorTicks && template )
	{
		for ( var angle = this.minAngle; angle<=this.maxAngle; angle+=this.anglePerValue*this.majorTicks )
		{
			var item = ( angle == this.minAngle ) ? template : template.cloneNode( true );
			item.setAttribute( 'transform', 'rotate('+angle+')' );
			if ( item != template ) template.parentNode.appendChild( item );
			
			var text  = item.getElementByClassName( 'major-tick-label' );
			if ( text )
			{
				text.childNodes[0].nodeValue = this.valueFromAngle( angle );
			}
		}
	}
	
	this.hands = { };
}

SVGWeather.Dial.prototype.valueFromAngle = function( angle )
{
	return (angle - this.minAngle) / this.anglePerValue + this.minValue;
}

SVGWeather.Dial.prototype.angleFromValue = function( value )
{
	return ( value - this.minValue ) * this.anglePerValue + this.minAngle;
}

SVGWeather.Dial.prototype.setValue = function( handClass, value )
{
	var hand = this.hands[ handClass ];
	if ( !hand )
	{
		hand = this.hands[ handClass ] = this.svg.getElementByClassName( handClass );
	}
	hand.handValue = value;
	
	var displayAngle = Math.min( this.maxAngle, Math.max( this.minAngle, this.angleFromValue(value) ) );
	hand.setAttribute( 'transform', 'rotate('+displayAngle+')' );
	//hand.svg.setAttribute( 'opacity', 1 - 0.1 * handNumber );
}

SVGWeather.Dial.prototype.getValue = function( handClass )
{
	var hand = this.hands[ handClass ];
	if ( !hand )
	{
		hand = this.hands[ handClass ] = this.svg.getElementByClassName( handClass );
	}
	return (hand && hand.handValue) || 0;
}

SVGWeather.Dial.defaults = {
	scale					: 1,	  // factor from default
	radius				: 100,  // may use SVG units
	centerX				: 0,	  // may use SVG units
	centerY				: 0,	  // may use SVG units
	minValue			: 0,	  // minimum value
	maxValue			: 100,  // maximum value
	minAngle			: -150,	// Starting angle (degrees clockwise from north)				 
	maxAngle			:  150, // ending angle	 (degrees clockwise from north)
};

Object.prototype.mergeWith = function( otherObject )
{
	for( var key in otherObject )
	{
		if( otherObject.hasOwnProperty(key) && (this[key] == null) )
		{
			this[key] = otherObject[key];
		}
	}
	return this;
}

SVGWeather.createSVG = function( kind, properties, parent, text )
{
	var xlinkNS = /^xlink:/;
	var svg=document.createElementNS("http://www.w3.org/2000/svg",kind);
	if ( properties != null )
	{
		for ( var attr in properties )
		{
			if ( properties.hasOwnProperty( attr ) )
			{
				var ns = xlinkNS.test( attr ) ? 'http://www.w3.org/1999/xlink' : null;
				svg.setAttributeNS( ns, attr, properties[ attr ] );
			}
		}
	}
	if ( parent ) parent.appendChild( svg );
	if ( text ) svg.appendChild( document.createTextNode(text) );
	return svg;
}

document.getElementsByClassName = 
Element.prototype.getElementsByClassName = function( className )
{
	var regex = new RegExp( "\\b"+className+"\\b" );
	var descendents = this.getElementsByTagName( "*" );
	var matches = [], ct=0;
	for ( var i=0, len=descendents.length; i<len; ++i )
	{
		var item = descendents[ i ];
		if ( regex.test( item.getAttribute('class') ) ) matches[ ct++ ] = item;
	}
	return matches;
}

document.getElementByClassName = 
Element.prototype.getElementByClassName = function( className )
{
	var regex = new RegExp( "\\b"+className+"\\b" );
	var descendents = this.getElementsByTagName( "*" );
	for ( var i=0, len=descendents.length; i<len; ++i )
	{
		var item = descendents[ i ];
		if ( regex.test( item.getAttribute('class') ) ) return item;
	}
	return null;
}
